<?php
namespace Admin\Controller;

class ProductController extends CommonController
{
	// 首页
	public function index()
	{
		$num = I('get.num') ? I('get.num') : 5;
		$model = D('prodetail');
		$count = $model -> count();
		// 分页
		$page = new \Think\Page($count,$num);
		$page -> rollPage = 5;
		$page -> lastSuffix = false;
		$page -> setConfig('prev', '上一页');
		$page -> setConfig('next', '下一页');
		$page -> setConfig('last', '末页');
		$page -> setConfig('first', '首页');
		$show = $page -> show();
		$data = $model -> order('id DESC') -> limit($page -> firstRow, $page -> listRows) -> select();
		// 搜索的时候清空session
		session('admin.foodname', null);
		session('admin.issale', null);
		$this -> assign('data', $data);
		$this -> assign('show', $show);
		// 显示多少页
		$this -> assign('num', $num);
		$this -> display();
	}
	// 商品信息的导出
	public function importOut()
	{
		vendor('PHPExcel.Classes.PHPExcel');
		$excel = new \PHPExcel();
		//Excel表格式
		$letter = array('A','B','C','D','E','F','G','H');
		//表头数组
		$tableheader = array('id','菜品名称','菜品图片','菜品介绍','菜品价格','是否在售');
		//填充表头信息
		for($i = 0;$i < count($tableheader);$i++) {
			$excel->getActiveSheet()->setCellValue("$letter[$i]1","$tableheader[$i]");
			}
		// 获取数据表中的数据
		$model = D('prodetail');
		if(session('admin.foodname')) {
			$data  = $model -> where("foodname like '%".session('admin.foodname')."%' and issale = '".session('admin.issale')."'") -> select();
		}else{
			$data  = $model -> select();
		}
		//填充表格信息
		for ($i = 2;$i <= count($data) + 1;$i++) {
			$j = 0;
			foreach ($data[$i - 2] as $key=>$value) {
				$excel->getActiveSheet()->setCellValue("$letter[$j]$i","$value");
				$j++;
			}
		}
		//创建Excel输入对象
		$write = new \PHPExcel_Writer_Excel5($excel);
		header("Pragma: public");
		header("Expires: 0");
		header("Cache-Control:must-revalidate, post-check=0, pre-check=0");
		header("Content-Type:application/force-download");
		header("Content-Type:application/vnd.ms-execl");
		header("Content-Type:application/octet-stream");
		header("Content-Type:application/download");;
		header('Content-Disposition:attachment;filename="testdata.xls"');
		header("Content-Transfer-Encoding:binary");
		$write->save('php://output');
	}
	// 新增商品
	public function add()
	{
		if(IS_POST) {
			$file = $_FILES['file'];
			$input_data = I('post.');
			$model = D('prodetail');
			// 自动验证
			$verify = $model -> create($input_data);
			if(!$verify) {
				$this -> error($model->getError(), U('admin/product/add'), 2);
			}else{
				$result = $model -> saveData($input_data, $file);
			}
			// 判断是否插入成功
			if($result) {
				$url = U('admin/product/index');
				echo "<script>alert('添加成功！');window.location.href='$url'</script>";
			}else{
				$this ->error('插入数据失败！',U('admin/product/add'),2);
			}
		}else{
			$this -> display();
		}
	}
	// 查询
	public function search()
	{

		$num = I('get.num') ? I('get.num') : 5;
		$inputData = I('get.');
		$foodname = I('get.foodname');
		if($foodname == false){
			$inputData = array('foodname' => session('admin.foodname'), 'issale' => session('admin.issale'));
		};
		session('admin.foodname', $inputData['foodname']);
		session('admin.issale', $inputData['issale']);
		$model = D('prodetail');
		$count = $model -> searchData($inputData);
		// 分页
		$page = new \Think\Page($count,$num);
		$page -> rollPage = 5;
		$page -> lastSuffix = false;
		$page -> setConfig('prev', '上一页');
		$page -> setConfig('next', '下一页');
		$page -> setConfig('last', '末页');
		$page -> setConfig('first', '首页');
		$show = $page -> show();
		$data = $model -> where("foodname like '%$inputData[foodname]%' and issale = '$inputData[issale]'") -> order('id DESC') -> limit($page -> firstRow, $page -> listRows) -> select();		
		$this -> assign('data', $data);
		$this -> assign('show', $show);
		// 显示多少页
		$this -> assign('num', $num);
		$this -> display();
	}
	// 删除
	public function delData()
	{
		$deleteId = I('get.id');
		$model = D('prodetail');
		$result = $model -> deleteOne($deleteId);
		if($result) {
			$this -> success('删除成功！', '/admin/product/index', 2);
		}else {
			$this -> error('删除失败！', '/admin/product/index', 2);
		}
	}
	// 编辑方法
	public function editData()
	{
		if(IS_POST){
			$inputData = I('post.');
			$file = $_FILES['file'];
			// 实例模型
			$model = D('prodetail');
			$result = $model -> editResult($inputData, $file);
			if($result) {
				$this -> success('编辑成功！', '/admin/product/index', 2);
			}else {
				$this -> error($model -> getError(), '/admin/product/index', 2);
			}
		}else {
			$editId = I('get.id');
			$model = D('prodetail');
			$result = $model -> edit($editId);
			$this -> assign('result', $result);
			$this -> display();
		}
	}
}